package cn.lele.reggie.controller;

import cn.lele.reggie.common.BaseContext;
import cn.lele.reggie.common.R;
import cn.lele.reggie.entity.AddressBook;
import cn.lele.reggie.entity.User;
import cn.lele.reggie.service.AddressBookService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * 地址簿管理
 */
@Slf4j
@RestController
@RequestMapping("/addressBook")
public class AddressBookController {
    @Autowired
    private AddressBookService addressBookService;

    /**
     * 列出地址信息
     * @return
     */
    @GetMapping("/list")
    public R<List> list(){
        Long userId = (Long) BaseContext.getCurrentId();
        LambdaQueryWrapper<AddressBook> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(AddressBook::getUserId,userId);
        List<AddressBook> list = addressBookService.list(wrapper);
        return R.success(list);
    }

    /**
     * 添加地址
     * @param addressBook
     * @return
     */
    @PostMapping
    public R<String> insert(@RequestBody AddressBook addressBook){
        Object userId = BaseContext.getCurrentId();
        addressBook.setUserId((Long) userId);
        addressBookService.save(addressBook);
        return R.success("添加地址成功");
    }

    /**
     * 回显地址信息
     * @param id
     * @return
     */
    @GetMapping("{id}")
    public R<AddressBook> selectById(@PathVariable Long id){
        AddressBook addressBook = addressBookService.getById(id);
        return R.success(addressBook);
    }

    /**
     * 删除地址
     * @param ids
     * @return
     */
    @DeleteMapping
    public R<String> delete(Long[] ids){
        for (Long id : ids) {
            addressBookService.removeById(id);
        }
        return R.success("删除地址成功");
    }

    /**
     * 修改地址
     * @param addressBook
     * @return
     */
    @PutMapping
    public R<String> update(@RequestBody AddressBook addressBook){
        addressBookService.updateById(addressBook);
        return R.success("修改地址成功");
    }

    /**
     * 改默认地址
     * @param addressBook
     * @return
     */
    @PutMapping("/default")
    public R<String> updateDefault(@RequestBody AddressBook addressBook){
        //将默认的改为不默认
        //LambdaQueryWrapper<AddressBook> wrapper = new LambdaQueryWrapper<>();
        LambdaUpdateWrapper<AddressBook> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(AddressBook::getUserId,(Long)BaseContext.getCurrentId());
//        for (AddressBook book : addressBookService.list(wrapper)) {
//            book.setIsDefault(0);
//            addressBookService.updateById(book);
//        }
        //优化
        wrapper.set(AddressBook::getIsDefault,0);
        addressBookService.update(wrapper);
        //将当前的地址改为默认
        addressBook.setIsDefault(1);
        addressBookService.updateById(addressBook);
        return R.success("修改默认地址成功");
    }

    @GetMapping("/default")
    public R<AddressBook> selectDefault(){
        Long userId = BaseContext.getCurrentId();
        LambdaQueryWrapper<AddressBook> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(AddressBook::getUserId,userId).eq(AddressBook::getIsDefault,1);
        AddressBook one = addressBookService.getOne(wrapper);
        return R.success(one);
    }

}
